var apiurl = "https://tianqiapi.com/api?version=epidemic&appid=13229176&appsecret=zhTPQsT3"
var nhwurl = "http://fx.yechenggame.com/index/index/feiyan"
var https = require("https")
var http = require("http")
var epidemicServer = function() {
	this.datas = {
		"date" : "",
		"today" : {
			"diagnosed" : 0,
			"suspect" : 0,
			"death" : 0,
			"cured" : 0,
		},
		"yesterinc": {
			"diagnosed" : 0,
			"suspect" : 0,
			"death" : 0,
			"cured" : 0,
		},
		"areas" : {}
	}
	this.nhwDatas = {
		"date" : "",
		"today" : {
			"diagnosed" : 0,
			"suspect" : 0,
			"death" : 0,
			"cured" : 0,
		},
		"yesterinc": {
			"diagnosed" : 0,
			"suspect" : 0,
			"death" : 0,
			"cured" : 0,
		},
		"areas" : {}
	}
}
var local = {}
//创建新账号
epidemicServer.prototype.init = function() {
	this.disposeData()
	this.disposeNhwData()
	setInterval(this.disposeData.bind(this),10 * 60 * 1000)
	setInterval(this.disposeNhwData.bind(this),10 * 60 * 1000)
	
}
//获取并处理数据
epidemicServer.prototype.disposeData = function() {
	var self = this
    var req=https.get(apiurl,function(res){
        var data = data
        res.on("data",function(chunk) {
          data += chunk
        })
        res.on("end",function() {
          data = data.replace("undefined","")
          data = JSON.parse(data)
          if(data.errcode == 0 && data.data){
          	for(var key in self.datas.today){
          		self.datas.today[key] = data.data[key]
          	}
          	self.datas.yesterinc["diagnosed"] = data.data.history[0]["confirmedNum"] - data.data.history[1]["confirmedNum"]
          	self.datas.yesterinc["suspect"] = data.data.history[0]["suspectedIncr"]
          	self.datas.yesterinc["death"] = data.data.history[0]["deathsNum"] - data.data.history[1]["deathsNum"]
          	self.datas.yesterinc["cured"] = data.data.history[0]["curesNum"] - data.data.history[1]["curesNum"]
			for(var i = 0;i < data.data.area.length;i++){
				let info = {
					"name" : data.data.area[i].provinceName,
					"today" : {
					"diagnosed" : data.data.area[i].confirmedCount,
					"suspect" : data.data.area[i].suspectedCount,
					"death" : data.data.area[i].deadCount,
					"cured" : data.data.area[i].curedCount
					},
					"yesterinc" : {
					"diagnosed" : data.data.area[i].yesterdayIncreased.confirmedCount,
					"suspect" : data.data.area[i].yesterdayIncreased.suspectedCount,
					"death" : data.data.area[i].yesterdayIncreased.deadCount,
					"cured" : data.data.area[i].yesterdayIncreased.curedCount
					}
				}
				self.datas.areas[data.data.area[i].locationId] = info
			}
			self.datas.date = data.data.date
          }
        })
    })
}
epidemicServer.prototype.getDataByLocationId = function(locationId) {
	let info = {
		today : this.datas.today,
		yesterinc : this.datas.yesterinc,
		area : this.datas.areas[locationId],
		date : this.datas.date
	}
	return info
}
//获取南海网数据并处理
epidemicServer.prototype.disposeNhwData = function() {
	var self = this
    var req=http.get(nhwurl,function(res){
        var data = data
        res.on("data",function(chunk) {
          data += chunk
        })
        res.on("end",function() {
			data = data.replace("undefined","")
			data = JSON.parse(data)
			if(data.status !== 200){
				console.log("disposeNhwData error ",data)
				return
			}
			self.nhwDatas.date = data.data.stat[0].update
			self.nhwDatas.today["diagnosed"] = Number(data.data.stat[0].list[0].today)
			self.nhwDatas.today["suspect"] = Number(data.data.stat[0].list[1].today)
			self.nhwDatas.today["death"] = Number(data.data.stat[0].list[3].today)
			self.nhwDatas.today["cured"] = Number(data.data.stat[0].list[2].today)
			self.nhwDatas.yesterinc["diagnosed"] = Number(data.data.stat[0].list[0].add)
			self.nhwDatas.yesterinc["suspect"] = Number(data.data.stat[0].list[1].add)
			self.nhwDatas.yesterinc["death"] = Number(data.data.stat[0].list[3].add)
			self.nhwDatas.yesterinc["cured"] = Number(data.data.stat[0].list[2].add)
			let info = {
				"name" : "海南",
				"date" : data.data.stat[1].update,
				"today" : {
					"diagnosed" : Number(data.data.stat[1].list[0].today),
					"suspect" : Number(data.data.stat[1].list[1].today),
					"death" : Number(data.data.stat[1].list[3].today),
					"cured" : Number(data.data.stat[1].list[2].today)
				},
				"yesterinc" : {
					"diagnosed" : Number(data.data.stat[1].list[0].add),
					"suspect" : Number(data.data.stat[1].list[1].add),
					"death" : Number(data.data.stat[1].list[3].add),
					"cured" : Number(data.data.stat[1].list[2].add)
				}
			}
			self.nhwDatas.areas[460000] = info
        })
    })
}
epidemicServer.prototype.getDataBynhw = function() {
	let info = {
		today : this.nhwDatas.today,
		yesterinc : this.nhwDatas.yesterinc,
		area : this.nhwDatas.areas[460000],
		date : this.nhwDatas.date
	}
	return info
}
module.exports = {
	id : "epidemicServer",
	func : epidemicServer,
	init : "init",
	lazy : true
}